package org.mparts.client.activity;

import org.mparts.client.ui.editor.IPartitemEditorView;
import org.mparts.client.ui.editor.IPartitemEditorView.IPartitemEditorPresenter;
import org.mparts.client.ui.editor.PartitemEditor.Driver;
import org.mparts.client.ui.events.ReloadPartitemListEvent;
import org.mparts.client.ui.events.SaveEvent;
import org.mparts.client.ui.events.SaveEventHandler;
import org.mparts.client.ui.events.ShowPartitemEvent;
import org.mparts.shared.MpartsRequestFactory;
import org.mparts.shared.proxy.AutoProxy;
import org.mparts.shared.proxy.ModelProxy;
import org.mparts.shared.proxy.PartgroupProxy;
import org.mparts.shared.proxy.PartitemProxy;
import org.mparts.shared.request.MpartsRequest;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.RequestContext;

public class PartitemActivity extends AbstractMpartsActivity implements IPartitemEditorPresenter {
	
	private IPartitemEditorView display;
	private EventBus eventBus;
	private Driver driver;
	private PartitemProxy currentEntity;

	@Inject
	public PartitemActivity(MpartsRequestFactory rf, EventBus eventBus, IPartitemEditorView display) {
		this.display = display;
		this.rf = rf;
		this.eventBus = eventBus;
		if ( display != null ) {			
			bind();
		}
	}
	
	private void bind() {
		display.setPresenter(this);
		driver = display.initDriver(rf);
//		driver = display.getDriver();
		eventBus.addHandler(SaveEvent.TYPE, new SaveEventHandler() {
			
			@Override
			public void onSave(SaveEvent saveEvent) {
				Log.debug("save edited partitem...");
				RequestContext req = driver.flush();
//				PartitemProxy item = driver.flush();
				PartgroupProxy grp = display.getGrpItem().getValue();
				if ( grp != null ) {
					currentEntity.setGrp(grp.getId());
					currentEntity.setGrpName(grp.getName());
				}
				AutoProxy auto = display.getAutoItem().getValue();
				if ( auto != null ) {
					currentEntity.setAuto(auto.getId());
					currentEntity.setAutoName(auto.getName());
				}
				ModelProxy model = display.getModelItem().getValue();
				if ( model != null ) {
					currentEntity.setModel(model.getId());
					currentEntity.setModelName(model.getName());
				}
//				MpartsRequest req = rf.mpartsRequest();
//				req.save(item);
				if (!driver.hasErrors()) {
					req.fire(new Receiver<Void>() {
						
						@Override
						public void onSuccess(Void response) {
							Log.debug("save successful");
							eventBus.fireEvent(new ReloadPartitemListEvent());
							display.getDialog().hide();
						}
					});
				}
			}
		});		
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		display.getDialog().show();
	}

	@Override
	public void editPartitem(PartitemProxy item) {
		MpartsRequest req = rf.mpartsRequest();
		display.clearFields();
		loadLists(display.getStoreGrp(), display.getStoreAuto());
		if ( item == null ) {
			currentEntity = req.create(PartitemProxy.class);
		} else {
			currentEntity = req.edit(item);

			//set combo values
			PartgroupProxy grp = display.getGrpItem().getStore().findModelWithKey(currentEntity.getGrp().toString());
			Log.debug("set grp: " + grp.getName());
			display.getGrpItem().setValue(grp, true);
			AutoProxy auto = display.getAutoItem().getStore().findModelWithKey(currentEntity.getAuto().toString());
			display.getAutoItem().setValue(auto, true);
			Scheduler.get().scheduleDeferred(new ScheduledCommand() {
				
				@Override
				public void execute() {
					ModelProxy model = display.getModelItem().getStore().findModelWithKey(currentEntity.getModel().toString());
					display.getModelItem().setValue(model, true);
				}
			});
		}
		req.savePartitem(currentEntity);
		
		driver.edit(currentEntity, req);		
		display.getDialog().show();
	}


}
